package com.imau.brand.controller;

import com.alibaba.fastjson.JSON;
import com.imau.brand.bean.Brand;
import com.imau.brand.common.entity.PageEntitry;
import com.imau.brand.service.BrandService;
import com.imau.brand.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// /brand/add

/**
 * 一、 请求控制端改造
 * <p>
 * 1. 改变请求路径，前缀固定 代表业务名称  后缀操作：add delete edit list find getinfo  /brand/*  /student/*  /user/*
 * 2. 对应到方法
 * 3. 写一个Servlet 控制器中
 */
@WebServlet("/brand/*")
public class BrandController extends BaseController {

    /**
     * 初始化业务层对象
     */
    private BrandService brandService = new BrandServiceImpl();

    /**
     * 品牌列表
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端分页参数
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        //2.获取查询条件

        String jsonQuery = request.getReader().readLine();
        Brand brand = JSON.parseObject(jsonQuery, Brand.class);
        //3.调用业务层方法
        PageEntitry<Brand> pageEntitry = brandService.selectPageByCondition(Integer.valueOf(currentPage), Integer.valueOf(pageSize), brand);
        //4.响应前端
        this.writeJson(response, pageEntitry, true);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端发送的数据
        String jsonString = request.getReader().readLine();
        //2.转成Java对象
        Brand brand = JSON.parseObject(jsonString, Brand.class);
        //3.调用业务层方法保存
        boolean b = brandService.saveBrand(brand);
        //4.响应结果
        this.writeJson(response, b);

    }

    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端发送的数据
        String jsonString = request.getReader().readLine();
        //2.转成Java对象
        Brand brand = JSON.parseObject(jsonString, Brand.class);
        //3.调用业务层方法保存
        boolean b = brandService.editBrand(brand);
        //4.响应结果
        this.writeJson(response, b);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端发送的数据
        String id = request.getParameter("id");

        //2.调用业务层方法保存
        boolean b = brandService.dleBrand(Integer.valueOf(id));
        //4.响应结果
        this.writeJson(response, b);
    }


    protected void deleteBatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strIds = request.getReader().readLine();
        Integer[] ids = JSON.parseObject(strIds, Integer[].class);
        boolean b = brandService.deleteBatchByIds(ids);

        this.writeJson(response, b);
    }

    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端发送的数据
        String id = request.getParameter("id");

        //2.调用业务层方法保存
        Brand brand = brandService.findById(Integer.valueOf(id));
        //4.响应结果
        this.writeJson(response, brand, true);

    }

}
